package com.itmentu.mapper;
import com.itmentu.pojo.UserInfo;
import com.itmentu.sql.DynamicSQL;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @Author：http://www.itmentu.com
 * @date： 2021/11/9 12:05
 * @Description：
 ***/
public interface UserInfoMapper {

    /**
     * 增加用户
     */
    @Insert("insert into user_info(user_name,address,age) values(#{username},#{address},#{age})")
    void add(UserInfo userInfo);

    /**
     * 修改用户
     */
    @Update("update user_info set address=#{address},age=#{age} where id=#{id}")
    void update(UserInfo userInfo);

    /**
     * 删除
     */
    @Delete("delete from user_info where id=#{id}")
    void deleteById(Integer id);

    /***
     * 查询
     */
    @Results(
            value = {
                @Result(property = "idCard",
                        column = "id_card",
                        one =@One(select = "com.itmentu.mapper.IdCardMapper.one",
                                fetchType = FetchType.LAZY)),
                @Result(property = "roles",
                        column = "id",
                        many = @Many(select = "com.itmentu.mapper.RolesMapper.userRoles",
                                fetchType = FetchType.LAZY))
            }
    )
    @Select("select * from user_info where id=#{id}")
    UserInfo findOne(Integer id);


    /**
     * 条件查询
     */
    @Select("<script>" +
            "SELECT * FROM user_info " +
            "<where>" +
            "<if test=\"username!=null and username!=''\">" +
            "AND user_name like concat('%',#{username},'%')" +
            "</if>" +
            "</where>" +
            "</script>")
    List<UserInfo> search(UserInfo userInfo);


    /**
     * 动态SQL
     * @param userInfo
     * @return
     */
    @SelectProvider(type = DynamicSQL.class,method = "findUserList")
    List<UserInfo> dynamicFindUser(UserInfo userInfo);

    /**
     * 动态SQL-insert
     * @param userInfo
     * @return
     */
    @SelectProvider(type = DynamicSQL.class,method = "inserUser")
    void dynamicAddUser(UserInfo userInfo);
}
